package com.keydom.ropeskipping.utils.banner;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.PagerAdapter;

import com.keydom.ropeskipping.R;
import com.keydom.ropeskipping.dot.StudentHomeworkDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.example.testproject.utils.banner
 * @ClassName: BannerAdapter
 * @Author: szj
 * @CreateDate: 8/23/21 1:05 PM
 */
public class BannerAdapter extends PagerAdapter {
    private List<StudentHomeworkDTO> mData;
    private final List<Integer> mPositions = new ArrayList<>();
    private Map<Integer, Boolean> selectMap = new HashMap<>();
    private boolean isRefresh = false;

    public void setRefresh(boolean refresh) {
        isRefresh = refresh;
    }

    public void setSelected(Map<Integer, Boolean> selectMap) {
        this.selectMap.clear();
        this.selectMap = selectMap;
    }

    public void setmPositions(List<Integer> positions) {
        mPositions.clear();
        mPositions.addAll(positions);
    }

    public BannerAdapter(List<StudentHomeworkDTO> data) {
        this.mData = data;
    }

    @Override
    public int getCount() {
//        return Integer.MAX_VALUE;// 返回无限个
        return mData.size();// 返回数据的个数
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {//子View显示
        View view = View.inflate(container.getContext(), R.layout.banner_item_layout, null);
        ImageView imageView = view.findViewById(R.id.iv_icon);
        LinearLayout linerLayout1 = view.findViewById(R.id.linerLayout1);
        LinearLayout linerLayout2 = view.findViewById(R.id.linerLayout2);
        TextView textNull = view.findViewById(R.id.text_null);
        TextView textSecond = view.findViewById(R.id.text_second);
        TextView viewCenter = view.findViewById(R.id.view_center);
        TextView textNum = view.findViewById(R.id.text_num);
        TextView textSecond1 = view.findViewById(R.id.text_second1);
        View viewCenter1 = view.findViewById(R.id.view_center1);
        TextView textNum1 = view.findViewById(R.id.text_num1);
        TextView textPosition = view.findViewById(R.id.text_position);

        textPosition.setText(String.valueOf(position));
        if (position == 0 || position == getCount() - 1) {
            imageView.setImageResource(R.mipmap.icon_list_item);
            linerLayout1.setVisibility(View.GONE);
            linerLayout2.setVisibility(View.GONE);
            textNull.setVisibility(View.VISIBLE);
        } else {
            if (mData.get(position) != null) {

                if (mData.get(position).isSelected()) {
                    imageView.setImageResource(R.mipmap.icon_list_selcted_item);
                } else {
                    imageView.setImageResource(R.mipmap.icon_list_item);
                }
                linerLayout1.setVisibility(View.VISIBLE);
                linerLayout2.setVisibility(View.VISIBLE);
                textNull.setVisibility(View.GONE);

                if (mData.get(position).getType() == 0) {
                    //计时计数
                    textSecond.setVisibility(View.VISIBLE);
                    viewCenter.setVisibility(View.VISIBLE);
                    textNum.setVisibility(View.VISIBLE);
                    textSecond1.setVisibility(View.VISIBLE);
                    viewCenter1.setVisibility(View.VISIBLE);
                    textNum1.setVisibility(View.VISIBLE);
                    textSecond.setText(String.valueOf(mData.get(position).getSportSeconds()));
                    textNum.setText(String.valueOf(mData.get(position).getSportCount()));
                } else if (mData.get(position).getType() == 1) {
                    //计时
                    textSecond.setVisibility(View.VISIBLE);
                    viewCenter.setVisibility(View.GONE);
                    textNum.setVisibility(View.GONE);
                    textSecond1.setVisibility(View.VISIBLE);
                    viewCenter1.setVisibility(View.GONE);
                    textNum1.setVisibility(View.GONE);
                    textSecond.setText(String.valueOf(mData.get(position).getSportSeconds()));
                } else {
                    //计数
                    textSecond.setVisibility(View.GONE);
                    viewCenter.setVisibility(View.GONE);
                    textNum.setVisibility(View.VISIBLE);
                    textSecond1.setVisibility(View.GONE);
                    viewCenter1.setVisibility(View.GONE);
                    textNum1.setVisibility(View.VISIBLE);
                    textNum.setText(String.valueOf(mData.get(position).getSportCount()));
                }
            }
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick();
            }
        });
        container.addView(view);//添加到父控件
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;// 过滤和缓存的作用
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);//从viewpager中移除掉
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        if (isRefresh) {
            return POSITION_NONE;
        }
        int position = Integer.parseInt(((TextView)((ConstraintLayout) object).getViewById(R.id.text_position)).getText().toString());
        if (mPositions.contains(position)) {
            Log.e("TAG", "更新position: " + position);
            if (selectMap.containsKey(position)) {
                if (selectMap.get(position)) {
                    ((ImageView)((ConstraintLayout) object).getViewById(R.id.iv_icon)).setImageResource(R.mipmap.icon_list_selcted_item);
                } else {
                    ((ImageView)((ConstraintLayout) object).getViewById(R.id.iv_icon)).setImageResource(R.mipmap.icon_list_item);
                }
            }
        }
        return super.getItemPosition(object);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick();
    }
}
